﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Resn.WhuPub.BLL;
using System.Linq;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// SearchCategoryWindow.xaml 的交互逻辑
        /// </summary>
        public partial class SearchBranchWindow
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                public SearchBranchWindow()
                {
                        InitializeComponent();
                }

                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                        _gm.ProvinceString = string.Empty;
                        _gm.CityString = string.Empty;

                        LayoutRoot.DataContext = _gm;
                }

                private void Window_OnMouseDown(object sender, MouseButtonEventArgs e)
                {
                        if (e.ChangedButton == MouseButton.Left)
                        {
                                this.DragMove();
                        }
                }

                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        _gm.ProvinceString = _gm.ProvinceString.Trim();
                        _gm.CityString = _gm.CityString.Trim();

                        try
                        {
                                var result = _gm.Branchs.AsEnumerable();

                                
                                if (!string.IsNullOrEmpty(_gm.ProvinceString))
                                {
                                        result = result.Where(i => i.Province.Contains(_gm.ProvinceString));
                                }

                                if (!string.IsNullOrEmpty(_gm.CityString))
                                {
                                        result = result.Where(i => i.City.Contains(_gm.CityString));
                                }

                                _gm.Branchs = new ObservableCollection<Branch>(result);
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        Close();
                }

                
        }
}